package baseFx.common.utils;

import baseFx.common.collections.StringKeyMap;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlUtils {
    private static Pattern hostPat = Pattern.compile("([a-zA-z]+://)?(?<host>[^\\s]*?)(/.*|$)", Pattern.CASE_INSENSITIVE);

    public static String encode(String input) {
        try {
            return URLEncoder.encode(input, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decode(String input) {
        try {
            return URLDecoder.decode(input, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static StringKeyMap<String> toMap(String input) {
        return toMap(input, true);
    }

    public static StringKeyMap<String> toMap(String input, boolean decode) {
        StringKeyMap<String> params = new StringKeyMap<>();
        if (StringUtils.isNullOrEmpty(input)) {
            return params;
        }
        if (input.contains("?")) {
            input = input.substring(input.indexOf("?") + 1);
        }
        String[] items = input.split("&");
        for (String item : items) {
            String[] kv = item.split("=");
            if (kv.length > 1) {
                params.put(kv[0], decode ? decode(kv[1]) : kv[1]);
            } else if (kv.length == 1) {
                params.put(kv[0], "");
            }
        }
        return params;
    }

    public static String join(Map params) {
        return join(params, true);
    }

    public static String join(Map params, boolean encode) {
        List<String> list = new ArrayList<>();
        for (Object k : params.keySet()) {
            Object v = params.get(k);
            if (v == null) {
                v = "";
            }
            if (encode) {
                list.add(k + "=" + encode(v.toString()));
            } else {
                list.add(k + "=" + v);
            }
        }
        return String.join("&", list);
    }

    public static String join(String url, Map params) {
        if (params != null && params.size() > 0) {
            url += url.contains("?") ? "&" : "?";
            url += join(params);
        }
        return url;
    }

    public static String join(String url, String... params) {
        if (params != null && params.length > 0) {
            url += url.contains("?") ? "&" : "?";
            url += String.join("&", params);
        }
        return url;
    }

    public static String home(String url) {
        int s = url.indexOf("//");
        if (s > 0) {
            s = url.indexOf("/", s + 2);
            if (s > 0) {
                return url.substring(0, s);
            } else {
                return url;
            }
        }
        return null;
    }

    public static String host(String url) {
        Matcher matcher = hostPat.matcher(url);
        if (matcher.find()) {
            return matcher.group("host");
        }
        return null;
    }

    public static String domain(String url) {
        url = host(url);
        if (url == null) {
            return null;
        }
        return url.contains(":") ? url.substring(0, url.indexOf(":")) : url;
    }

//    public static void main(String[] args) {
//        String url = "https://blog.csdn.net:80/jiaobuchong/article/details/81257570";
//        System.out.println(host(url));
//        System.out.println(domain(url));
//    }
}
